#!/bin/bash
alias mv='mv' # 取消强制覆盖时的确认
alias echo='echo -e'

SRCQUOTA="/${QUOTA_TEST}/srcQuotaDir"
Q_SRCDIR="${FS_MOUNTPOINT}${SRCQUOTA}" # Q_ 开头的目录代表创建了配额

DESTQUOTA="/${QUOTA_TEST}/destQuotaDir"
Q_DESTDIR="${FS_MOUNTPOINT}${DESTQUOTA}"

NQ_PARENTDIR="${FS_MOUNTPOINT}/${QUOTA_TEST}/parent_noquota" # NQ_ 开头的目录代表没有设置配额的目录

CHILDQUOTA="/${QUOTA_TEST}/parent_noquota/childQuotaDir"
Q_CHILDDIR="${NQ_PARENTDIR}/childQuotaDir"

global_used=0

msg0="测试所有用例"
msg1="rename时目标目录不能存在(vfs报错)"
msg2="不能 rename/mv 配额目录本身"
msg3="不能 rename/mv 配额目录的父目录"
msg4="源目录和目标目录都不属于配额目录" 
msg5="源目录和目标目录都被设置配额, 且属于同一配额目录"
msg6="源目录和目标目录都被设置配额, 但分别属于两个不同的配额目录, 验证目标配额未超出"
msg7="源目录和目标目录都被设置配额, 但分别属于两个不同的配额目录, 验证目标配额超出"
msg8="源目录属于配额目录，目标目录不属于配额目录，配额不会超出"
msg9="源目录不属于配额目录，目标目录属于配额目录，验证目标配额未超出"
msg10="源目录不属于配额目录，目标目录属于配额目录，验证目标配额超出"
maxmsg=10

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${maxmsg}; num++ ));do
        eval echo "'    'sh test_one.sh ${name} ${num} '  ' \${msg${num}}"
    done
    exit
}

function clean_all_dir() {
    # 创建非配额目录, 并创建其子目录，后续为子目录创建配额
    func_rmdir ${NQ_PARENTDIR}; mkdir -p ${NQ_PARENTDIR}; mkdir -p ${Q_CHILDDIR}

    echo "=====删除然后重新创建目录 ${Q_SRCDIR} 和 ${Q_DESTDIR}====="
    func_rmdir ${Q_SRCDIR}; orcafs-ctl --createdir --nodes=1001 ${Q_SRCDIR}
    func_rmdir ${Q_DESTDIR}; orcafs-ctl --createdir --nodes=2001 ${Q_DESTDIR}

    # 检查两个配额目录是否位于不同 meta 节点
    srcMeta=`orcafs-ctl --getentryinfo ${Q_SRCDIR} | grep Metadata | awk '{print $3}'`
    descMeta=`orcafs-ctl --getentryinfo ${Q_DESTDIR} | grep Metadata | awk '{print $3}'`
    echo "${Q_SRCDIR} : ${srcMeta}"
    echo "${Q_DESTDIR} : ${descMeta}"
    if [ ${srcMeta} == ${descMeta} ]; then echo "${Q_SRCDIR} 和 ${Q_DESTDIR} 在同一个 ${srcMeta}"; exit 1; fi
}

function reset_all() {
    clean_quota; #sleep ${WAIT_CLIENT_SYNC};
    clean_all_dir;
    
    orcafs-ctl --setquota --dir=${Q_SRCDIR} --hardlimit=10M &> /dev/null
    orcafs-ctl --setquota --dir=${Q_DESTDIR} --hardlimit=5M &> /dev/null
    orcafs-ctl --setquota --dir=${Q_CHILDDIR} --hardlimit=5M &> /dev/null
    sleep ${WAIT_CLIENT_SYNC};

    echo -n "=====清空目录并重新创建配额目录 [${Q_SRCDIR}] [${Q_DESTDIR}] [${Q_CHILDDIR}] ====="
    quota_num=`orcafs-ctl --listquota | grep -w DIR | wc -l`
    if ((${quota_num} == 3));then echo "pass"; else echo "not pass"; exit 1; fi; echo "";
}

function v1_diffquota_rename_to_existdir() {
    cmd="mkdir -p ${Q_SRCDIR}/dir_exist"; echo "\tExec: ${cmd}"; eval ${cmd}
    cmd="mkdir -p ${Q_DESTDIR}/dir_exist"; echo "\tExec: ${cmd}"; eval ${cmd}
    cmd="mv ${Q_SRCDIR}/dir_exist ${Q_DESTDIR}";echo "\tExec: ${cmd}"; eval ${cmd} &>${LOGFILE}
    
    msg=`cat ${LOGFILE}`; echo "\t$msg";
    grep "File exists" ${LOGFILE} &>/dev/null; res=$?

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${res} == 0)); then echo " 验证成功\n"; else echo "  验证失败\n"; exit 1; fi
}

function v2_diffquota_rename_quotaself() {
    cmd="mv $Q_SRCDIR $Q_DESTDIR";echo "\tExec: ${cmd}"; eval ${cmd} &>${LOGFILE}
    msg=`cat ${LOGFILE}`; echo "\t$msg"; 
    grep "Device or resource busy" ${LOGFILE} &>/dev/null; res=$?

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${res} == 0)); then echo " 验证成功\n"; else echo "  验证失败\n"; exit 1; fi
}

function v3_diffquota_rename_quota_parent() {
    cmd="mv ${NQ_PARENTDIR} ${Q_DESTDIR}";echo "\tExec: ${cmd}"; eval ${cmd} &>${LOGFILE}
    msg=`cat ${LOGFILE}`; echo "\t$msg";
    grep "Device or resource busy" ${LOGFILE} &>/dev/null; res=$?

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${res} == 0)); then echo " 验证成功\n"; else echo "  验证失败\n"; exit 1; fi
}

function v4_diffquota_rename_quota_parent() {
    func_cleandir ${NQ_PARENTDIR}
    mkdir -p ${NQ_PARENTDIR}/src
    cmd="mv ${NQ_PARENTDIR}/src ${NQ_PARENTDIR}/dest";echo "\tExec: ${cmd}"; eval ${cmd} &>${LOGFILE}
    res=$?
    echo -n "after mv:\n\t [ls ${NQ_PARENTDIR}]: "; ls ${NQ_PARENTDIR}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${res} == 0)); then echo " 验证成功\n"; else echo "  验证失败\n"; exit 1; fi
}

function v5_diffquota_rename_in_samequotadir() {
    #echo "==5===源目录和目标目录属于同一配额目录(配额使用量不变)"
    mkdir -p ${Q_SRCDIR}/fromdir
    dd if=/dev/zero of=${Q_SRCDIR}/fromdir/file1 bs=3M count=1 &> /dev/null

    old_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $8}'`
    echo -n "before mv(${old_used}):\n\t [ls ${Q_SRCDIR}]: "; ls ${Q_SRCDIR}

    echo -n "exec mv cmd:\n";
    cmd="mv ${Q_SRCDIR}/fromdir ${Q_SRCDIR}/todir";echo "\tExec: ${cmd}"; eval ${cmd}

    new_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $8}'`
    echo -n "after mv(${new_used}):\n\t [ls ${Q_SRCDIR}]: "; ls ${Q_SRCDIR}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${old_used} == ${new_used})); then echo "验证成功\n"; else echo " 验证失败\n"; exit 1; fi
}

function v6_rename_from_srcquota_to_destquota() {
    #echo "\n==6===源目录和目标目录都被设置配额, 但分别属于两个不同的配额目录, 验证目标配额未超出"
    func_cleandir ${Q_SRCDIR}
    mkdir -p ${Q_SRCDIR}/fromdir
    orcafs-ctl --createdir --nodes=1001 ${Q_SRCDIR}/fromdir/testdir1 &> /dev/null
    orcafs-ctl --createdir --nodes=1001 ${Q_SRCDIR}/fromdir/testdir2 &> /dev/null
    dd if=/dev/zero of=${Q_SRCDIR}/fromdir/file1 bs=2M count=1 &> /dev/null
    dd if=/dev/zero of=${Q_SRCDIR}/fromdir/testdir1/file2 bs=2M count=1 &> /dev/null
    dd if=/dev/zero of=${Q_SRCDIR}/fromdir/testdir2/file2 bs=1M count=1 &> /dev/null

    src_old_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $8}'`
    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $8}'`
    echo -n -e "before mv(${src_old_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "before mv(${dest_old_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    #exit
    echo -n -e "exec mv cmd:\n";
    cmd="mv ${Q_SRCDIR}/fromdir ${Q_DESTDIR}/todir";echo "\tExec: ${cmd}"; eval ${cmd}

    src_new_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $8}'`
    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $8}'`
    echo -n -e "after mv(${src_new_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "after mv(${dest_new_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${src_old_used} == ${dest_new_used})) && ((${src_new_used} == ${dest_old_used}));then
    echo "验证成功\n"; else echo " 验证失败\n"; exit 1; fi
}

function v7_rename_from_srcquota_to_destquota_exceed() {
    #echo "\n==7===源目录和目标目录都被设置配额, 但分别属于两个不同的配额目录, 验证目标配额超出"
    func_cleandir ${Q_SRCDIR}
    mkdir -p ${Q_SRCDIR}/fromdir
    dd if=/dev/zero of=${Q_SRCDIR}/fromdir/file1 bs=3M count=1 &> /dev/null
    dd if=/dev/zero of=${Q_SRCDIR}/fromdir/file2 bs=4M count=1 &> /dev/null

    src_old_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $8}'`
    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $8}'`
    echo -n -e "before mv(${src_old_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "before mv(${dest_old_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    echo -n -e "exec mv cmd:\n";
    cmd="mv ${Q_SRCDIR}/fromdir ${Q_DESTDIR}/todir";echo -n -e "\t${cmd}\n\t"; eval ${cmd}
    if (($? == 1)); then echo "\tDisk quota exceeded, 测试成功"; break; else echo " 验证失败\n"; exit 1;fi

    src_new_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $8}'`
    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $8}'`
    echo -n -e "after mv(${src_new_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "after mv(${dest_new_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${src_old_used} == ${src_new_used})) && ((${dest_new_used} == ${dest_old_used}));then
    echo "验证成功\n"; else echo " 验证失败\n"; exit 1; fi
}

function v8_rename_from_srcquota_to_noquota() {
    #echo "\n==8===源目录属于配额目录，目标目录不属于配额目录，配额不会超出"
    func_cleandir ${Q_SRCDIR}
    mkdir -p ${Q_SRCDIR}/fromdir
    dd if=/dev/zero of=${Q_SRCDIR}/fromdir/file1 bs=3M count=1 &> /dev/null
    dd if=/dev/zero of=${Q_SRCDIR}/fromdir/file2 bs=2M count=1 &> /dev/null

    src_old_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $8}'`
    echo -n -e "before mv(${src_old_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    echo -n -e "exec mv cmd:\n";
    cmd="mv ${Q_SRCDIR}/fromdir ${NQ_PARENTDIR}/todir";echo "\tExec: ${cmd}"; eval ${cmd}
    #if (($? == 1)); then echo "\tDisk quota exceeded, 测试成功"; break; else echo " 验证失败\n"; exit 1;fi

    src_new_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $8}'`
    echo -n -e "after mv(${src_new_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${src_old_used} != 0)) && ((${src_new_used} == 0));then
    echo "验证成功\n"; else echo " 验证失败\n"; exit 1; fi
}

function v9_rename_from_noquota_to_destquota() {
    #echo "\n==9===源目录不属于配额目录，目标目录属于配额目录，验证目标配额未超出"
    mkdir -p ${NQ_PARENTDIR}/fromdir
    rm -rf ${Q_DESTDIR}/*
    dd if=/dev/zero of=${NQ_PARENTDIR}/fromdir/file1 bs=1M count=1 &> /dev/null
    dd if=/dev/zero of=${NQ_PARENTDIR}/fromdir/file2 bs=1M count=1 &> /dev/null

    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $8}'`
    echo -n -e "before mv(${dest_old_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}
    echo -n -e "\t [ls ${NQ_PARENTDIR}]: "; ls -a ${NQ_PARENTDIR}

    echo -n -e "exec mv cmd:\n";
    cmd="mv ${NQ_PARENTDIR}/fromdir ${Q_DESTDIR}/todir";echo "\tExec: ${cmd}"; eval ${cmd}
    if (($? == 1)); then echo "\tDisk quota exceeded,  验证失败\n"; exit 1; fi

    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $8}'`
    echo -n -e "after mv(${dest_new_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}
    echo -n -e "\t [ls ${NQ_PARENTDIR}]: "; ls -a ${NQ_PARENTDIR}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${dest_new_used} != 0)) && ((${dest_old_used} == 0));then
    echo "验证成功\n"; else echo " 验证失败\n"; exit 1; fi
}

function v10_rename_from_noquota_to_destquota_exceed() {
    #echo "\n==10===源目录不属于配额目录，目标目录属于配额目录，验证目标配额超出"
    mkdir -p ${NQ_PARENTDIR}/fromdir
    func_cleandir ${NQ_PARENTDIR}
    func_cleandir ${Q_DESTDIR}
    dd if=/dev/zero of=${NQ_PARENTDIR}/fromdir/file1 bs=3M count=1 &> /dev/null
    dd if=/dev/zero of=${NQ_PARENTDIR}/fromdir/file2 bs=4M count=1 &> /dev/null

    orcafs-ctl --listquota
    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $8}'`
    echo -n -e "before mv(${dest_old_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}
    echo -n -e "\t [ls ${NQ_PARENTDIR}]: "; ls -a ${NQ_PARENTDIR}

    echo -n -e "exec mv cmd:\n";
    cmd="mv ${NQ_PARENTDIR}/fromdir ${Q_DESTDIR}/todir";echo -n -e "\t${cmd}\n\t"; eval ${cmd}
    if (($? == 1)); then echo "\tDisk quota exceeded, 测试成功"; break; else echo " 验证失败\n"; exit 1;fi

    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $8}'`
    echo -n -e "after mv(${dest_new_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}
    echo -n -e "\t [ls ${NQ_PARENTDIR}]: "; ls -a ${NQ_PARENTDIR}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if ((${dest_new_used} == 0)) && ((${dest_old_used} == 0));then
    echo "验证成功\n"; else echo " 验证失败\n"; exit 1; fi
}

echo "/************************************************************************"
echo "测试在不同配额目录内 rename/mv 目录"
echo "************************************************************************/"
if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi
source ${QUOTA_COMMON_FUNC}

reset_all;

test_num=1; if (($1 == ${test_num})) || (($1 == 0));then v1_diffquota_rename_to_existdir; fi
test_num=2; if (($1 == ${test_num})) || (($1 == 0));then v2_diffquota_rename_quotaself; fi
test_num=3; if (($1 == ${test_num})) || (($1 == 0));then v3_diffquota_rename_quota_parent; fi
test_num=4; if (($1 == ${test_num})) || (($1 == 0));then v4_diffquota_rename_quota_parent; fi
test_num=5; if (($1 == ${test_num})) || (($1 == 0));then v5_diffquota_rename_in_samequotadir; fi
test_num=6; if (($1 == ${test_num})) || (($1 == 0));then v6_rename_from_srcquota_to_destquota; fi
test_num=7; if (($1 == ${test_num})) || (($1 == 0));then v7_rename_from_srcquota_to_destquota_exceed; fi
test_num=8; if (($1 == ${test_num})) || (($1 == 0));then v8_rename_from_srcquota_to_noquota; fi
test_num=9; if (($1 == ${test_num})) || (($1 == 0));then v9_rename_from_noquota_to_destquota; fi
test_num=10; if (($1 == ${test_num})) || (($1 == 0));then v10_rename_from_noquota_to_destquota_exceed; fi

func_cleandir ${FS_MOUNTPOINT}/${QUOTA_TEST}
echo "/******************************************************************************"
echo "测试在不同配额目录内 rename/mv 目录==== all success"
echo "******************************************************************************/"
alias mv='mv -i' # 强制覆盖时需要确认
exit 0;
